package org.example.lease.controller;

import org.example.common.login.LoginUserHolder;
import org.example.lease.model.result.Result;
import org.example.lease.service.LoginService;
import org.example.lease.vo.login.CaptchaVo;
import org.example.lease.vo.login.LoginVo;
import org.example.lease.vo.system.user.SystemUserInfoVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/25 9:54
 */
@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 给前端返回一个图形验证码
     *
     * @return 结果包含图形验证码
     */
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        CaptchaVo captchaVo = loginService.getCaptcha();
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("/info")
    public Result<SystemUserInfoVo> info() {
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        SystemUserInfoVo loginUserInfo = loginService.getLoginUserInfo(userId);
        return Result.ok(loginUserInfo);
    }
}